package team;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * User Interface for adding new part
 * @author Sean Peng
 *
 */
public class AddNewPartFrame extends JFrame{

	private JFrame frame = this;
	private Map<String, JTextField> inputMap = new HashMap<String, JTextField>();
	
	
	/**
	 * Constructor for AddNewPartFrame
	 * @param partModFrame
	 */
	public AddNewPartFrame(final JFrame partModFrame) {
		
		String[] vendors = Result.getVendor();
		final String[] colNames = Result.getColumnName(Result.table_RDIMARS);
		final JComboBox vendorBox = new JComboBox(vendors);
		final ArrayList<String> inputData = new ArrayList<String>();
		
		JPanel addPanel = new JPanel();
		JButton addButton = new JButton("Add");
		JButton clrButton = new JButton("Clear");
		JButton backButton = new JButton("Back");
		
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(inputValidator()) {
					
					inputData.clear();
					for(String s : colNames) {
						inputData.add(inputMap.get(s).getText().toUpperCase());
					}
					
					if( !Result.insertNewPart((String)vendorBox.getSelectedItem(), inputData) )
						JOptionPane.showMessageDialog(null, "Part number already existed", "Error", JOptionPane.ERROR_MESSAGE);
				}
				else
					JOptionPane.showMessageDialog(null, "Invalide Input", "Error", JOptionPane.ERROR_MESSAGE);
			}
			
		});
		
		clrButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				//Clear the text fields	
				ArrayList<JTextField> textField = new ArrayList<JTextField>(inputMap.values());
				for(JTextField tf : textField)
					tf.setText("");
			}
		});
		
		backButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				partModFrame.setVisible(true);
				frame.dispose();
			}
			
		});
		
		
		addPanel.setLayout(new GridLayout(colNames.length + 1, 2));		
		addPanel.add(new JLabel("Vendor"));
		addPanel.add(vendorBox);
		
		for(int i=0; i<colNames.length; i++) {
			
			JTextField textField = new JTextField();
			
			addPanel.add(new JLabel(colNames[i]));
			addPanel.add(textField);
			inputMap.put(colNames[i], textField);
		}
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(addButton);
		buttonPanel.add(clrButton);
		buttonPanel.add(backButton);
		
		frame.add(addPanel, BorderLayout.CENTER);
		frame.add(buttonPanel, BorderLayout.SOUTH);
		
		frame.setTitle("Add Part");
		frame.setSize(400,400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
	} //End of Constructor
	
	
	
	/**
	 * Constructor for AddNewPartFrame
	 * @param addNewAutoFrame
	 * @param rLink
	 */
	public AddNewPartFrame(final JFrame addNewAutoFrame, final Integer rLink) {
		
		String[] vendors = Result.getVendor();		
		final JComboBox vendorBox = new JComboBox(vendors);
		final String[] colNames = Result.getColumnName(Result.table_RDIMARS);
		final ArrayList<String> inputData = new ArrayList<String>();
		
		JPanel addPanel = new JPanel();
		JButton addButton = new JButton("Add");
		JButton clrButton = new JButton("Clear");
		JButton backButton = new JButton("Back");
		
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(inputValidator()) {
					
					inputData.clear();
					for(String s : colNames) {
						inputData.add(inputMap.get(s).getText().toUpperCase());
					}
					System.out.println(inputData.get(0));
					if( !Result.insertNewPart((String)vendorBox.getSelectedItem(), inputData, rLink) )
						JOptionPane.showMessageDialog(null, "Part number already existed", "Error", JOptionPane.ERROR_MESSAGE);
				}
				else
					JOptionPane.showMessageDialog(null, "Invalide Input", "Error", JOptionPane.ERROR_MESSAGE);
			}
			
		});
		
		clrButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				//Clear the text fields	
				ArrayList<JTextField> textField = new ArrayList<JTextField>(inputMap.values());
				for(JTextField tf : textField)
					tf.setText("");
			}
		});
		
		backButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				
				addNewAutoFrame.setVisible(true);
				frame.dispose();
			}
			
		});
		
		
		addPanel.setLayout(new GridLayout(colNames.length + 1, 2));		
		addPanel.add(new JLabel("Vendor"));
		addPanel.add(vendorBox);
		
		for(int i=0; i<colNames.length; i++) {
			
			JTextField textField = new JTextField();
			
			addPanel.add(new JLabel(colNames[i]));
			addPanel.add(textField);
			inputMap.put(colNames[i], textField);
		}
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(addButton);
		buttonPanel.add(clrButton);
		buttonPanel.add(backButton);
		
		frame.add(addPanel, BorderLayout.CENTER);
		frame.add(buttonPanel, BorderLayout.SOUTH);
		
		frame.setTitle("Add Part");
		frame.setSize(400,400);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private boolean inputValidator() {
		
		if( inputMap.get("P_NUMBER").getText().isEmpty() )
			return false;
		
		try {
			Integer.parseInt( inputMap.get("P_NUMBER").getText() );
			Double.parseDouble( inputMap.get("PRICE").getText() );
			Integer.parseInt( inputMap.get("AMOUNT").getText() );
		} catch(NumberFormatException e) {
			return false;
		}
		
		if(inputMap.get("P_NUMBER").getText().length() >= 4)
			return false;
		
		return true;
	}
}
